﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhSystemBLL.basicData;
using baseSet.importExport;
using dhsystem.Main;
using baseSet.power;
using dhDataAcess.dataAcess;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;


namespace dhsystem.basicForm
{
    public partial class FrmItemlist : Form
    {
        dhDataAcess.dataAcess.sysDataSetTableAdapters.itemListTableAdapter itemTableAdapter = new dhDataAcess.dataAcess.sysDataSetTableAdapters.itemListTableAdapter();
        dhDataAcess.dataAcess.sysDataSet item = new dhDataAcess.dataAcess.sysDataSet();
        itemListBLL itemList = new itemListBLL();
        string state;
        string count;
        string moduleNum;
        public FrmItemlist()
        {
            InitializeComponent();
        }
        //打开窗口时显示列表数据
        private void FrmItemlist_Load(object sender, EventArgs e)
        {
            itemTableAdapter.FillitemListAll(item.itemList);
            dGriditemlist.DataSource = item.itemList;

           bindingSourceitemList.DataSource = itemList.getitemListAll();
            dGriditemlist.DataSource = bindingSourceitemList;
           DcolumnSet.colSet(moduleInfo .itemList ,dGriditemlist );
           toolStripStatusLabel1.Text ="数据总条数:"+dGriditemlist.RowCount.ToString()+"条";
           count ="Item_"+(dGriditemlist.RowCount+1).ToString();
           //FrmItemlist items=new FrmItemlist ();
           //powerSet.modulPower( moduleInfo.itemList,items);

           sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.itemList);
           foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
           {
               buttonQuery.Enabled = modulAaccessRow.showAc;
               toolAdd.Enabled = modulAaccessRow.addAc;
               toolEdit.Enabled = modulAaccessRow.editAc;
               toolDel.Enabled = modulAaccessRow.delAc;
               toolPrint.Enabled = modulAaccessRow.printAc;
               toolExport.Enabled = modulAaccessRow.exportAc;
               toolimport.Enabled = modulAaccessRow.importAc;

           }

        }
        //新增按钮，显示新增窗口
        private void toolAdd_Click(object sender, EventArgs e)
        {
            state = "新增";
            FrmitemlistAdd itemListAdd = new FrmitemlistAdd(bindingSourceitemList, state, count);
            itemListAdd.ShowDialog();
            
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.itemList );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGriditemlist.DataSource = query.rTable;
            }
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            moduleNum = dGriditemlist.Rows[dGriditemlist.CurrentRow.Index].Cells[1].Value.ToString();
            state = "编辑";
            FrmitemlistAdd itemListAdd = new FrmitemlistAdd(bindingSourceitemList , state, count, moduleNum);
            itemListAdd.ShowDialog();
           
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string itemNum = dGriditemlist.Rows[dGriditemlist.CurrentRow.Index].Cells[2].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除列表：" + itemNum + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceitemList.DataSource = itemList.itemListDel(itemNum);
                    dGriditemlist.DataSource = bindingSourceitemList;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
           // bindingSourceitemList.DataSource = itemList.getitemListAll();
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSourceitemList.DataSource = itemList.getitemListAll("%" + textitemNumQuery.Text + "%"); 
                dGriditemlist.DataSource = bindingSourceitemList;

              
                //dGriditemlist.DataSource = itemList .getitemListAll ("%" + textitemNumQuery .Text  + "%");
                //bindingSourceitemList.DataSource = dGriditemlist.DataSource;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            

        }
        //刷新按钮
        private void buttupdate_Click(object sender, EventArgs e)
        {
            bindingSourceitemList.DataSource = itemList.getitemListAll();
            dGriditemlist.DataSource = bindingSourceitemList;
            itemTableAdapter.FillitemListAll(item.itemList);
            dGriditemlist.DataSource = item.itemList;
            toolStripStatusLabel1.Text = "数据总条数:" + dGriditemlist.RowCount.ToString() + "条";
        }
        //工具栏退出按钮，退出窗口
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //修改按钮
        private void buttModify_Click(object sender, EventArgs e)
        {

            try
            {
                itemTableAdapter.Update(item.itemList);
                MessageBox.Show("修改成功");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }  
        }
        //datagridview第一列显示行号
        private void dGriditemlist_RowStateChanged_1(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }
        //工具栏导出按钮
        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.itemList , dGriditemlist , "模块表格信息");
            frm.ShowDialog();
        }
        //工具栏导入按钮
        private void toolimport_Click(object sender, EventArgs e)
        {
            FrmImport import = new FrmImport(moduleInfo.itemList);
            import.ShowDialog();
        }

      }
    }

